package com.youi.shop.controller;


import com.youi.shop.commons.exceptions.GlobalException;
import com.youi.shop.commons.result.RespEntity;
import com.youi.shop.commons.result.StatusCode;
import com.youi.shop.pojo.entity.Product;
import com.youi.shop.pojo.qo.product.UserInsertProductQO;
import com.youi.shop.pojo.qo.product.UserUpdateProductQO;
import com.youi.shop.pojo.vo.product.ProductVO;
import com.youi.shop.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author xxf
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品业务接口")
public class ProductController {

    @Resource
    ProductService productService;

//-----------------------------查询业务----------------------------------

    /**
     * 分页查询商品
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/productList/{page}/{size}")
    @ApiOperation("分页展示所有商品")
    public RespEntity<List<ProductVO>> showByPage(@PathVariable int page, @PathVariable int size) {

        return productService.selectByPage(page, size);

    }

    /**
     * 查询所有商品
     *
     * @return
     */
    @GetMapping("/productList")
    @ApiOperation("展示所有商品")
    public RespEntity<List<ProductVO>> showAll() {

        return productService.selectAll();
    }

    /**
     * 按类别展示商品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/productList/{categoryId}")
    @ApiOperation("分类展示商品")
    public RespEntity<List<ProductVO>> showByCategory(@PathVariable Long categoryId) {

        return productService.selectByCategory(categoryId);
    }

    /**
     * 按id批量查询商品
     *
     * @param productIds
     * @return
     */
    @GetMapping("/selectproductByIds")
    @ApiOperation("按id批量查询商品")
    public RespEntity<List<ProductVO>> showByIds(@RequestParam List<Long> productIds) {

        return productService.selectByIds(productIds);
    }

    /**
     * 按名字模糊查询商品
     *
     * @param productName
     * @return
     */
    @GetMapping("/selectproductByName")
    @ApiOperation("按名字模糊查询商品")
    public RespEntity<List<ProductVO>> showByName(@RequestParam String productName) {

        return productService.selectByName(productName);
    }


//--------------------------------------添加业务--------------------------------------

    /**
     * 批量插入
     *
     * @param userInsertProductQOList
     * @return
     */
    @PostMapping("addProduct")
    @ApiOperation("批量添加商品")
    public RespEntity<Object> addProduct(@RequestBody List<UserInsertProductQO> userInsertProductQOList) {
        //遍历参数集合
        List<Product> list = new ArrayList<>();
        for (UserInsertProductQO userInsertProductQO : userInsertProductQOList) {
            Product product = new Product();
            BeanUtils.copyProperties(userInsertProductQO, product);
            list.add(product);
        }
        //添加到product实体类中批量插入数据库
        boolean flag = productService.saveBatch(list);
        //插入成功
        if (flag) {
            return RespEntity.success(null);
            //插入失败
        } else {
            throw new GlobalException(StatusCode.PRODUCT_INSERT_ERROR);
        }

    }


//--------------------------------------修改业务--------------------------------------

    /**
     * 修改单个商品信息
     *
     * @param userUpdateProductQO
     * @return
     */
    @PostMapping("modifyProduct")
    @ApiOperation("修改商品信息")
    public RespEntity<ProductVO> modifyProduct(@RequestBody UserUpdateProductQO userUpdateProductQO) {

        boolean flag = false;
        flag = productService.updateProduct(userUpdateProductQO);
        if (flag) {
            //修改成功
            return RespEntity.success(null);
        } else {
            //修改失败
            throw new GlobalException(StatusCode.PRODUCT_UPDATE_ERROR);
        }
    }

//--------------------------------------删除业务--------------------------------------

    /**
     * 批量删除商品信息
     *
     * @param productIds
     * @return
     */
    @PostMapping("deleteProducts")
    @ApiOperation("批量删除商品信息")
    public RespEntity<Object> deleteByIds(@RequestParam List<Long> productIds) {

        boolean flag = false;
        flag = productService.deleteProduct(productIds);
        if (flag) {
            //删除成功
            return RespEntity.success(null);

        } else {
            //删除失败
            throw new GlobalException(StatusCode.PRODUCT_DETELE_ERROR);
        }
    }


}
